//
//  XSHomeViewController.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/29.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSHomeViewController.h"
#import "XSVideoEditeViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <CoreMedia/CoreMedia.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "XSImagesToVideoViewController.h"
#import "DeveloperVC.h"

//test
#import "XSVideRecordViewController.h"
#import "XSAudioListView.h"

@interface XSHomeViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *videoEditeBtn;
@property (weak, nonatomic) IBOutlet UIButton *imageEditBtn;
@property (weak, nonatomic) IBOutlet UIButton *imagesToVideoBtn;

@property (weak, nonatomic) IBOutlet UIButton *shootBtn;



@end

@implementation XSHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    
    UITapGestureRecognizer *testTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toDeveloperVC)];
    testTap.numberOfTapsRequired = 3;
    [self.view addGestureRecognizer:testTap];
     
}

- (void)toDeveloperVC{
    DeveloperVC *developerVC = [[DeveloperVC alloc]init];
    [self.navigationController pushViewController:developerVC animated:YES];
      
}
 
- (void)setupUI{
    CGFloat radius = 10;
    self.videoEditeBtn.layer.cornerRadius = radius;
    self.videoEditeBtn.layer.masksToBounds = YES;
    
    self.imageEditBtn.layer.cornerRadius = radius;
    self.imageEditBtn.layer.masksToBounds = YES;
    
    self.imagesToVideoBtn.layer.cornerRadius = radius;
    self.imagesToVideoBtn.layer.masksToBounds = YES;
 
    self.shootBtn.layer.cornerRadius = radius;
    self.shootBtn.layer.masksToBounds = YES;
}


#pragma mark - 视频编辑
- (IBAction)pickVideoFromPhtotos:(id)sender {

    [XSPhotosManager showImagePickerController:self selectVideo:YES maxCount:9 complete:^(NSArray<XSPhotoModel *> *array) {
        XSPhotoModel *modle = [array firstObject];
        if (modle.type == XSAssetMediaTypeVideo) {
            if (array.count > 1) {
                //合并
                [XSAVTools mergeVideos:array complete:^(int status, NSString *errorMsg, NSURL *mergeFinishVideoURL) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                         XSVideoClipViewController *editVC = [XSVideoClipViewController new];
                        editVC.videoURL = mergeFinishVideoURL;
                        [self.navigationController pushViewController:editVC animated:NO];
                    });
                }];
            }else if (array.count == 1){
                XSVideoClipViewController *editVC = [XSVideoClipViewController new];
                editVC.photoModel = modle;
                [self.navigationController pushViewController:editVC animated:NO];
            }

            
            
        }else{
            
        }
        
    }];
    
}
#pragma mark - 图片编辑
- (IBAction)pickFromPhoto:(id)sender {
 
    [XSPhotosManager showImagePickerController:self selectVideo:NO complete:^(NSArray<XSPhotoModel *> *array) {
        XSPhotoModel *modle = [array firstObject];
        if (modle.type == XSAssetMediaTypeVideo) {
            
        }else{
            XSImageEditViewController *imageVC = [XSImageEditViewController new];
            imageVC.photoModel = modle;
            [self.navigationController pushViewController:imageVC animated:NO];
        }
        
    }];
    
}

#pragma mark - shoot
- (IBAction)takePhoto:(id)sender {
     XSVideRecordViewController *editVC = [XSVideRecordViewController new];
     [self.navigationController pushViewController:editVC animated:YES];
  
}

#pragma mark - 图片合成
- (IBAction)imagesToVideo:(id)sender {
    
    [XSPhotosManager showImagePickerController:self selectVideo:NO maxCount:30 complete:^(NSArray<XSPhotoModel *> *array) {
        
        XSImagesToVideoViewController *toVideoVC = [[XSImagesToVideoViewController alloc] initWithNibName:@"XSImagesToVideoViewController" bundle:nil];
        toVideoVC.imageModels = array;
        [self.navigationController pushViewController:toVideoVC animated:YES];
    }];
    
}


- (void)pickVideoFromSystem{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeMovie, nil];
    imagePickerController.delegate = self;
    imagePickerController.editing = NO;
    imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    XSVideoEditeViewController *editVC = [XSVideoEditeViewController new];
    editVC.videoURL = url;
    [self.navigationController pushViewController:editVC animated:YES];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    [super touchesBegan:touches withEvent:event];
    
//    [self pickVideoFromSystem];
 
//    XSVideoEditeViewController *editVC = [XSVideoEditeViewController new];
//    [self.navigationController pushViewController:editVC animated:YES];
    
    
   
 
}





@end
